<template>
  <div class="container">
    <div class="app-container">
      <div class="edit-form">
        <el-form ref="form" :model="formData" :rules="formRules" label-width="220px">
          <!-- 姓名 -->
          <el-row>
            <el-col :span="12">
              <el-form-item label="姓名" prop="username">
                <el-input v-model="formData.username" size="mini" class="inputW" />
              </el-form-item>
            </el-col>
          </el-row>
          <!-- 工号  -->
          <el-row>
            <el-col :span="12">
              <el-form-item label="工号" prop="worknumber">
                <el-input v-model="formData.workNumber" disabled size="mini" class="inputW" />
              </el-form-item>
            </el-col>
          </el-row>
          <!-- 手机  -->
          <el-row>
            <el-col :span="12">
              <el-form-item label="手机" prop="mobile">
                <el-input
                  v-model="formData.mobile"
                  size="mini"
                  class="inputW"
                  :disabled="getEmployId?true:false"
                />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <!-- 部门 -->
            <el-col :span="12">
              <el-form-item label="部门" prop="departmentId">
                <!-- 放置级联部门组件  -->
                <select-tree v-model="formData.departmentId" class="inputW" />
                <!-- 会生效在组件中的最外层的盒子 -->
              </el-form-item>
            </el-col>
          </el-row>
          <!-- 聘用形式 -->
          <el-row>
            <el-col :span="12">
              <el-form-item label="聘用形式" prop="formOfEmployment">
                <el-select
                  v-model="formData.formOfEmployment"
                  size="mini"
                  class="inputW"
                >
                  <el-option label="正式" :value="1" />
                  <el-option label="非正式" :value="2" />
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <!-- 入职时间 -->
          <el-row>
            <el-col :span="12">
              <el-form-item label="入职时间" prop="timeOfEntry">
                <el-date-picker
                  v-model="formData.timeOfEntry"
                  size="mini"
                  type="date"
                  value-format="yyyy-MM-dd"
                  class="inputW"
                />
              </el-form-item>
            </el-col>
          </el-row>
          <!-- 转正时间 -->
          <el-row>
            <el-col :span="12">
              <el-form-item label="转正时间" prop="correctionTime">
                <el-date-picker
                  v-model="formData.correctionTime"
                  size="mini"
                  type="date"
                  class="inputW"
                />
              </el-form-item>
            </el-col>
          </el-row>
          <!-- 员工头像 -->
          <el-row>
            <el-col :span="12">
              <el-form-item label="员工照片">
                <!-- 上传照片 -->
                <image-upload v-model="formData.staffPhoto" />
              </el-form-item>
            </el-col>
          </el-row>
          <!-- 保存个人信息 -->
          <el-row>
            <el-col :span="12" style="margin-left: 220px">
              <el-button size="mini" type="primary" @click="saveData">
                保存更新</el-button>
            </el-col>
          </el-row>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
import { addEmployee, getEmployeeDetail, setEmployeeDetail } from '@/api/employee'
import imageUpload from './components/image-upload.vue'
import selectTree from './components/select-tree.vue'
export default {
  name: 'Detail',
  components: {
    selectTree,
    imageUpload
  },
  data() {
    return {
      formData: {
        username: '', // 姓名
        mobile: '', // 电话
        formOfEmployment: null, // 聘用形式
        timeOfEntry: '', // 入职时间
        workNumber: '', // 工号
        correctionTime: '', // 转正时间
        departmentId: null, // 部门id
        staffPhoto: '' // 员工头像
      },
      formRules: {
        username: [{ required: 'true', message: '请输入姓名', trigger: 'blur' },
          { min: 1, max: 4, message: '姓名为1-4位', trigger: 'blur' }
        ],
        mobile: [{ required: 'true', message: '请输入手机号', trigger: 'blur' },
          { pattern: /^1[3-9]\d{9}$/, message: '请输入正确的手机号', trigger: 'blur' }
        ],
        departmentId: [{ required: true, message: '请选择部门', trigger: 'blur' }],
        timeOfEntry: [{ required: true, message: '请输选择入职时间', trigger: 'blur' }],
        correctionTime: [{ required: true, message: '请选择转正时间', trigger: 'blur' }, {
          validator: (rule, value, callback) => {
            if (this.formData.timeOfEntry) {
              if (new Date(this.formData.timeOfEntry) > new Date(value)) {
                // 比较时间早晚 new Date('2013-xx-11')  越晚越大
                callback('转正时间不能早于入职时间')
                return
              }
            }
            callback()
          }
        }]
      }
    }
  },
  computed: {
    getEmployId() {
      return this.$route.query.id
    }
  },
  created() {
    if (this.getEmployId) {
      this.getEmployeeDetail()
    }
  },
  methods: {
    saveData() {
      this.$refs.form.validate(async(isok) => {
        if (isok) {
          if (this.getEmployId) {
            await setEmployeeDetail(this.formData)
            this.$message.success('编辑成功')
          } else {
            await addEmployee(this.formData)
            this.$message.success('新增成功')
          }
          this.$router.push('/employee')
        }
      })
    },
    async getEmployeeDetail() {
      this.formData = await getEmployeeDetail(this.getEmployId)
    }
  }
}
</script>

  <style lang="scss" scoped>
  .edit-form{
      background: #fff;
      padding: 20px;
      .inputW{
          width:380px;
      }
  }

  </style>
